import 'package:flutter/material.dart';
import 'package:flutter_plugin/ext/get_extension.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../res/button_style.dart';
import '../../res/style.dart';
import '../behavior/over_scroll_behavior.dart';

/// @description :公共通用弹窗
class CommonDialog extends StatelessWidget {
  ///标题
  String title = '';

  ///内容
  String content = '';

  ///左侧文字
  String negaText = '';

  ///右侧文字
  String posiText = '';

  ///左侧事件
  VoidCallback? negaTap;

  ///右侧事件
  VoidCallback? posiTap;

  ///左侧是否隐藏
  bool negaVisible = true;

  ///右侧是否隐藏
  bool posiVisible = true;

  Widget? centerWidget;

  CommonDialog({
    Key? key,
    this.title = '',
    this.content = '',
    this.negaText = '',
    this.posiText = '',
    this.negaVisible = true,
    this.posiVisible = true,
    this.negaTap,
    this.posiTap,
    this.centerWidget,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Material(

        ///透明样式
        type: MaterialType.transparency,

        ///dialog居中
        child: Center(

            ///取消ListView滑动阴影
            child: ScrollConfiguration(
                behavior: OverScrollBehavior(),

                ///ListView 的shrinkWrap属性可适应高度（有多少占多少）
                child: ListView(
                  shrinkWrap: true,
                  children: [
                    ///背景及内容、边距、圆角等，必须包裹在ListView中
                    Container(
                      width: double.infinity,
                      margin: EdgeInsets.only(right: 38.w, left: 38.w),
                      child: Container(
                        decoration: const ShapeDecoration(
                          color: Colors.white,
                          shape: RoundedRectangleBorder(
                            borderRadius: BorderRadius.all(
                              Radius.circular(8),
                            ),
                          ),
                        ),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.start,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            ///标题、内容

                            title.isNotEmpty ? Box.hBox30 : Container(),
                            title.isNotEmpty
                                ? Text(
                                    title,
                                    textAlign: TextAlign.center,
                                    style: Styles.style_FF333333_16_w500,
                                  )
                                : Container(),
                            Padding(
                              padding: EdgeInsets.symmetric(horizontal: 17.w, vertical: 17.h),
                              child: centerWidget ??
                                  Text(
                                    content,
                                    textAlign: TextAlign.center,
                                    style: Styles.style_FF303030_14,
                                  ),
                            ),
                            DividerStyle.dividerHalf,

                            ///确定、取消按钮
                            Flex(
                              mainAxisAlignment: MainAxisAlignment.spaceAround,
                              direction: Axis.horizontal,
                              children: [
                                ///对半分
                                _buildTextButton(negaTap, negaText, negaVisible, Styles.style_FF333333_16),

                                /// 分割线
                                negaVisible && posiVisible ? DividerStyle.dividerVertical : Container(),
                                _buildTextButton(posiTap, posiText, posiVisible, Styles.textStyle_FFEB6002_16)
                              ],
                            )
                          ],
                        ),
                      ),
                    ),
                  ],
                ))));
  }

  ///此处使用方法重新创建的原因是因为此处不需要频繁更新，不会造成过度创建
  _buildTextButton(VoidCallback? tap, String text, bool show, TextStyle textStyle) {
    return Visibility(
      visible: show,
      child: Expanded(
        flex: 1,
        child: TextButton(
          onPressed: () {
            Get.dismiss();
            if (tap != null) {
              tap();
            }
          },
          style: ButtonStyles.getNoShapeStyle(),
          child: Container(
            alignment: Alignment.center,
            height: 40.h,
            child: Text(
              text,
              style: textStyle,
            ),
          ),
        ),
      ),
    );
  }
}
